
package jcoyote;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *  CoyoteBoT Main class
 *
 * @author  Lenouvel Baptiste
 * @author  http://opensource.org/licenses/gpl-license.php GNU Public License
 */
public class main {
    
    static private String    line ;
    static private String[]  event_type;
    static private String    regex = "^[ 0-9]{2}[0-9]:[0-9]{2}" ;
    static public  List<Q3Player> PlayerList = new ArrayList<Q3Player>();
  
    public static void main(String[] args) throws InterruptedException, FileNotFoundException, IOException, Exception {
       
     
       Init init = new Init();
       Server serveur = new Server("My Server", init.getServerIP(), init.getServerPort(), init.getServerRCon());
       RconQuery Query = new RconQuery(serveur);
       Pattern r = Pattern.compile(regex);
       PlayerList.clear();
       
       System.out.print("Ouverture du Log...");
       
       /** Ouverture du games.log en lecture seule */
       RandomAccessFile reader = new RandomAccessFile(init.getServerLoGPath(),"r");
       
       /** Placement du curseur a la fin du fichier */
       reader.seek(reader.length());
       
       System.out.println("[OUVERT]\n");
       System.out.println("CoyoteBoT Success ! \n");
       Query.bigText("CoyoteBoT Launched");
       Query.say("CoyoteBoT v"+init.version);
       
       
       
       while(true) {
           
           Thread.sleep(200);
       
            while((line = reader.readLine()) != null) {
                
                Matcher m = r.matcher(line);
                event_type = m.replaceAll("").substring(1).split(":");
                
                if(event_type[0].equals("ClientConnect")) {
                    clientConnect(event_type[1].substring(1));
                    
                }
                
                if(event_type[0].equals("ClientUserinfo")) {
                    clientUserinfo(line);
                }
                
            }
           
       
       
       }
           
    }

    private static void clientConnect(String id) {
        PlayerList.add(new Q3Player(id));
        System.out.println("Client connect, ID = " +id);
    }

    @SuppressWarnings("empty-statement")
    private static void clientUserinfo(String line) throws InterruptedException {
        
        /** 
         * Contenu du tableau splt
         * 
         * splt[0]  = [ ID ]
         * splt[2]  = [IP]
         * splt[4]  = [name]
         * splt[6]  = [password]
         * splt[12] = [rate]
         * splt[46] = [guid]
         * 
         **/
        Pattern r = Pattern.compile(regex);
        
        String[] splt = line.split("\\\\");
        int size = PlayerList.size(); // For win time in loop
        int i = -1;
        Q3Player plr = null ;
        String result = "";
        
        Matcher m = r.matcher(splt[0]);; 
        String[] tmp = m.replaceAll("").split(":");
        splt[0] = tmp[1].substring(1).replaceAll(" ", ""); // ClientUserinfo ID's

        do {
            i++;
            plr = PlayerList.get(i);
            result = plr.getId();
        }while( (result.equals(splt[0]) == false ) && i < size);
        
        if(result.equals(splt[0])) {
            plr.setId(splt[0]);
            plr.setIp(splt[2]);
            plr.setNick(splt[4]);
            plr.setRate(splt[12]);
            plr.setGuid(splt[46]);
            System.out.println("Actualisation infos joueur :");
            System.out.println(" Name : "+splt[4]);
            System.out.println(" ID : "+splt[0]);
            System.out.println(" IP : "+splt[2]);
            System.out.println(" Rate : "+splt[12]);
            System.out.println(" GUID : "+splt[46]+"\n\n");
            PlayerList.set(i, plr);
        }
        else {
            System.out.println("Player Not Found ! @ClientUserinfo event.");
            Thread.sleep(2000);
            System.exit(1);
        }
        
    }
    

    
    
}